package com.fjnu.springmvc.controller;

import com.fjnu.springmvc.vo.ActivityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/th")
@Slf4j
public class ActivityController {


   @GetMapping("/activity")
    public String queryActivity(HttpSession session,  Model model) {
       model.addAttribute("message",
                "response from thymeleaf controller!") ;
       model.addAttribute("hello", "你好！");

       ActivityVO activityVO = new ActivityVO() ;
       activityVO.setName("夏令营活动") ;
       activityVO.setStartTime(new Date());
       // save an object in context/model
       model.addAttribute("activity", activityVO) ;
       // save common data in session
       // send list data
       List<ActivityVO> activities = new ArrayList<>() ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       activities.add(new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;

       model.addAttribute("activities", activities) ;
       session.setAttribute("activities", activities) ;
        return "query-activities";
    }

    @GetMapping("/add-activity")
    public String addActivity(HttpSession session,
                              Model model,
                              ActivityVO activity) {
        return "add-activities";

    }

    @GetMapping("/delete")
    public String deleteActivity(HttpSession session,
                                  Model model,
                                  String name) {

       List<ActivityVO> activites = (List<ActivityVO>) session.getAttribute("activities") ;
        Assert.notEmpty(activites, "活动列表为空");
        for (ActivityVO activityVO : activites) {
            if (activityVO.getName().equals(name)) {
                activites.remove(activityVO);
                break;
            }
        }
        session.setAttribute("activities", activites);
        model.addAttribute("activities", activites);
//        model.addAttribute("activity", new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
       return "query-activities";

    }

    @PostMapping("/add")
    public String add(HttpSession session, HttpServletRequest request,
                                 Model model,
                      ActivityVO activity) {
        String name = request.getAttribute("name").toString();
        activity.setName(name);
        List<ActivityVO> activites = (List<ActivityVO>) session.getAttribute("activities") ;
        activites.add(activity);
        session.setAttribute("activities", activites);
        model.addAttribute("activities", activites);
//        model.addAttribute("activity", new ActivityVO("活动1","活动1","活动描述", new Date(), new Date())) ;
        return "query-activities";

    }


}
